/*
  @author ayh_lscaatbear
  @create 2021年7月1日
*/

let switchFlg  = false;
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
    if (msg.action === "backInfo") {
        let infoObj = msg.value
        $('#show-info').css('display', 'block')
        $('#show-error').css('display', 'none')
        if(infoObj.title){
            $('#video-title').text(infoObj.title)
        }else{
            $('#video-title').text(infoObj.tit)
        }
        if(infoObj.duration){
            $('#time-str').text(infoObj.duration)
        }else{
            $('#time-str').text(infoObj.time)
        }
        $('#spend-str').text(infoObj.spend + "倍数")
        switchFlg = infoObj.switchFlg
        if(switchFlg){
            $('#switch-button').css({'left':'17px'})
            $('#switch-bg').css({'width':'38px'})
        }else{
            $('#switch-button').css({'left':'1px'})
            $('#switch-bg').css({'width':'0px'})
        }
    }else if(msg.action === "showInfo"){
        if(msg.value){
            $('#show-info').css('display', 'block')
            $('#show-error').css('display', 'none')
        }else{
            $('#show-info').css('display', 'none')
            $('#show-error').css('display', 'block')
        }
    }else if(msg.action === 'returnSpend'){ 
        $('#spend-str').text(msg.value + "倍数")
    }
});


$('.switch-wrap').click(e=>{
    if(!switchFlg){
        $('#switch-button').css({'left':'17px'})
        $('#switch-bg').css({'width':'38px'})
        chrome.tabs.executeScript(null,{code:"addautoChange()"})
        switchFlg = !switchFlg
    }else{
        $('#switch-button').css({'left':'1px'})
        $('#switch-bg').css({'width':'0px'})
        chrome.tabs.executeScript(null,{code:"removeChange()"})
        switchFlg = !switchFlg
    }
})
$("#palyUp").mousedown(e=>{
    $("#palyUp").css({'transform':'scale(0.9)'})
})
$("#slowSpend").mousedown(e=>{
    $("#slowSpend").css({'transform':'scale(0.9)'})
})
$("#reset-spend").mousedown(e=>{
    $("#reset-spend").css({'transform':'scale(0.9)'})
})

$("#palyUp").mouseup(e=>{
    $("#palyUp").css({'transform':'scale(1)'})
})
$("#slowSpend").mouseup(e=>{
    $("#slowSpend").css({'transform':'scale(1)'})
})
$("#reset-spend").mouseup(e=>{
    $("#reset-spend").css({'transform':'scale(1)'})
})

$("#palyUp").click(e=>{
    chrome.tabs.executeScript(null,{code:"playUp()"})
})

$("#slowSpend").click(e=>{
    chrome.tabs.executeScript(null,{code:"playDown()"})
})

$("#reset-spend").click(e=>{
    chrome.tabs.executeScript(null,{code:"resetSpend()"})
})

chrome.runtime.sendMessage({
    action: 'getInfo'
});



// $('#palyUp').text($('#viewbox_report').find(".tit").text())





